package lx.af.demo;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

import lx.af.R;
import lx.af.base.BaseActivity;
import lx.af.manager.VolleyManager;
import lx.af.utils.log.Log;

/**
 * demo for com.google.volley. see detail:
 * https://android.googlesource.com/platform/frameworks/volley
 */
public class ActivityVolleyStringDemo extends BaseActivity
        implements BaseActivity.ActionBarImpl, View.OnClickListener {

    private static final String TAG = ActivityVolleyStringDemo.class.getSimpleName();

    private TextView mTvInfo;

    private StringRequest mRequestWeather = new StringRequest(
            "http://www.weather.com.cn/data/cityinfo/101120701.html",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    mTvInfo.setText(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    mTvInfo.setText(error.getMessage());
                    Log.e("liuxu", error.getMessage(), error);
                }
            }
    );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley_demo);
        mTvInfo = getView(R.id.avd_info);
        // scroll ability for TextView
        mTvInfo.setMovementMethod(ScrollingMovementMethod.getInstance());

        getView(R.id.avd_btn_request_douban).setOnClickListener(this);
        getView(R.id.avd_btn_request_weather).setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // cancel all pending requests when not in foreground
        VolleyManager.cancelPendingRequests(TAG);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.avd_btn_request_douban: {
                VolleyManager.addRequest(RequestDoubanString.getInstance(mTvInfo), TAG);
                break;
            }
            case R.id.avd_btn_request_weather: {
                VolleyManager.addRequest(mRequestWeather, TAG);
                break;
            }
        }
    }

    private static class RequestDoubanString extends StringRequest {

        public static RequestDoubanString getInstance(final TextView tv) {
            return new RequestDoubanString(
                new Response.Listener<String>() {
                @Override
                    public void onResponse(String response) {
                        tv.setText(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("liuxu", error.getMessage(), error);
                        tv.setText(error.getMessage());
                    }
                });
        }

        public RequestDoubanString(Response.Listener<String> listener,
                                   Response.ErrorListener errorListener) {
            super("http://api.douban.com/v2/book/1220562", listener, errorListener);
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            // use header to fake as Mozilla browser, since douban will
            // block the request if not from browser.
            Map<String, String> map = new HashMap<>();
            map.put("User-Agent",
                    "Mozilla/4.0 (" +
                            "compatible; MSIE 6.0; Windows NT 5.1;" +
                            "SV1; QQDownload 1.7; .NET CLR 1.1.4322;" +
                            "CIBA; .NET CLR 2.0.50727)");
            return map;
        }
    }

}
